package com.jqutil.jqutil.cy.utils;


import com.jqutil.jqutil.cy.common.Constant;
import com.jqutil.jqutil.cy.entity.ReadData;
import com.jqutil.jqutil.cy.entity.WriteData;

import java.util.*;

/**
 * @Author: chenyun
 * @Date: 2021/09/07/14:57
 * @Description:
 */
public class DataUtils {

    // 用于存放所有的周数
    static public HashSet<String> hashSet = new HashSet<>();

//    // 存放周数信息 和节次信息一起组合   用于解析写入格式  看是否已经存在相同周数和节次信息
//    static public HashSet<String> weekNumSet = new HashSet<>();
//
//    // 存放节次信息
//    static public HashSet<String> courseNumSet = new HashSet<>();
     static public HashSet<String> weekAndCourseSet = new HashSet<>();

//     static public HashSet<WriteData> writeDataList = new HashSet<>();
     static public List<WriteData> writeDataList = new ArrayList<WriteData>();

    /**
     * 将读取到数据解析成写入数据格式
     * @param readDataList
     * @return
     */
//    public static List<WriteData> parseReadDataListToWriteData(List<ReadData> readDataList) {
//
////        Collection<List<String>> mapValues = new ArrayList<>();
//        HashMap<String, List<String>> map = new HashMap<>();
//        List<Integer> weekRange = DataUtils.weekRange();
//        int maxWeek = weekRange.get(weekRange.size() - 1);
//        ReadData classroomName = DataUtils.getClassroomName(Read.classroomName);
//
//        for (int i = 0; i < maxWeek; i++) {
//            // 解析数据
//            for (ReadData readData : readDataList) {
//                // 获取教室和周数的对应关系 {4-617=[7, 8, 9], 实615=[11, 12]}
//                map = readData.getClassNameAndListWeek();
//                // 获取所有的key
//                Set<String> keySet = map.keySet();
//                Iterator<String> iterator = keySet.iterator();
//                while (iterator.hasNext()) {
//                    // 获取教室号里面的周数
//                    String cName = iterator.next();
//                    List<String> list = map.get(cName);
//                    for (String weekCount : list) {
//                        // 假如周数和节次不存在就创建新的对象  并且添加进集合
////                        if (!weekAndCourseSet.contains(weekCount+readData.getCourseNum())) {
////                            WriteData writeData = new WriteData();
////                            StringBuffer buffer = new StringBuffer();
////                            weekAndCourseSet.add(weekCount+readData.getCourseNum());
////                            writeData.setWeekCount(weekCount); //设置周次
////                            writeData.setCourseNum(readData.getCourseNum()); // 设置节次
////                            String week = readData.getWeek();  // 星期几
////                            // 星期一
////                            if (week.equals(Constant.weekList.get(0))) {
////
////                                if(null !=writeData.getOne()){
////                                    writeData.setOne(writeData.getOne().append("/"+cName));
////                                }else {
////                                    writeData.setOne(buffer.append(cName));
////                                }
////                                writeDataList.add(writeData);
////                                continue;
////                            }
////                            if (week.equals(Constant.weekList.get(1))) {
////                                if(null !=writeData.getTwo()){
////                                    writeData.setTwo(writeData.getTwo().append("/"+cName));
////                                }else {
////                                    writeData.setTwo(buffer.append(cName));
////                                }
////                                writeDataList.add(writeData);
////                                continue;
////                            }
////                            if (week.equals(Constant.weekList.get(2))) {
////                                if(null !=writeData.getThree()){
////                                    writeData.setThree(writeData.getThree().append("/"+cName));
////                                }else {
////                                    writeData.setThree(buffer.append(cName));
////                                }
////                                writeDataList.add(writeData);
////                                continue;
////                            }
////                            if (week.equals(Constant.weekList.get(3))) {
////                                if(null !=writeData.getFour()){
////                                    writeData.setFour(writeData.getFour().append("/"+cName));
////                                }else {
////                                    writeData.setFour(buffer.append(cName));
////                                }
////                                writeDataList.add(writeData);
////                                continue;
////                            }
////                            if (week.equals(Constant.weekList.get(4))) {
////                                if(null !=writeData.getFive()){
////                                    writeData.setFive(writeData.getFive().append("/"+cName));
////                                }else {
////                                    writeData.setFive(buffer.append(cName));
////                                }
////                                writeDataList.add(writeData);
////                                continue;
////                            }
////                            if (week.equals(Constant.weekList.get(5))) {
////                                if(null !=writeData.getSex()){
////                                    writeData.setSex(writeData.getSex().append("/"+cName));
////                                }else {
////                                    writeData.setSex(buffer.append(cName));
////                                }
////                                writeDataList.add(writeData);
////                                continue;
////                            }
////                            if (week.equals(Constant.weekList.get(6))) {
////                                if(null !=writeData.getSeven()){
////                                    writeData.setSeven(writeData.getSeven().append("/"+cName));
////                                }else {
////                                    writeData.setSeven(buffer.append(cName));
////                                }
////                                writeDataList.add(writeData);
////                                continue;
////                            }
////
////                        }
//                        if (!weekAndCourseSet.contains(weekCount+readData.getCourseNum())) {
//                            WriteData writeData = new WriteData();
//                            StringBuffer buffer = new StringBuffer();
//                            HashSet<String> cNameHashSet = new HashSet<>();
//                            weekAndCourseSet.add(weekCount+readData.getCourseNum());
//                            writeData.setWeekCount(weekCount); //设置周次
//                            writeData.setCourseNum(readData.getCourseNum()); // 设置节次
//                            String week = readData.getWeek();  // 星期几
//                            // 星期一
//                            if (week.equals(Constant.weekList.get(0))) {
//                                cNameHashSet.add(cName);
//                                writeData.setOne(cNameHashSet);
//                                writeDataList.add(writeData);
//                                continue;
//                            }
//                            if (week.equals(Constant.weekList.get(1))) {
//                                cNameHashSet.add(cName);
//                                writeData.setTwo(cNameHashSet);
//                                writeDataList.add(writeData);
//                                continue;
//                            }
//                            if (week.equals(Constant.weekList.get(2))) {
//                                cNameHashSet.add(cName);
//                                writeData.setThree(cNameHashSet);
//                                writeDataList.add(writeData);
//                                continue;
//                            }
//                            if (week.equals(Constant.weekList.get(3))) {
//                                cNameHashSet.add(cName);
//                                writeData.setFour(cNameHashSet);
//                                writeDataList.add(writeData);
//                                continue;
//                            }
//                            if (week.equals(Constant.weekList.get(4))) {
//                                cNameHashSet.add(cName);
//                                writeData.setFive(cNameHashSet);
//                                writeDataList.add(writeData);
//                                continue;
//                            }
//                            if (week.equals(Constant.weekList.get(5))) {
//                                cNameHashSet.add(cName);
//                                writeData.setSex(cNameHashSet);
//                                writeDataList.add(writeData);
//                                continue;
//                            }
//                            if (week.equals(Constant.weekList.get(6))) {
//                                cNameHashSet.add(cName);
//                                writeData.setSeven(cNameHashSet);
//                                writeDataList.add(writeData);
//                                continue;
//                            }
//
//                    }
//                        else{
//                           // 列表中已经存在周数+节次
//                                    for (int j = 0; j < writeDataList.size(); j++) {
//                                        if(writeDataList.get(j).getWeekCount().equals(weekCount) && writeDataList.get(j).getCourseNum().equals(readData.getCourseNum())){
//                                            HashSet<String> cNameHashSet = new HashSet<>();
//                                            String week = readData.getWeek(); //周几
//                                            if (week.equals(Constant.weekList.get(0))) {
//
//                                                if(null !=writeDataList.get(j).getOne()){
//                                                    cNameHashSet = writeDataList.get(j).getOne();
//                                                    cNameHashSet.add("/"+cName);
//                                                    writeDataList.get(j).setOne(cNameHashSet);
//                                                }
//                                                continue;
//                                            }
//                                            if (week.equals(Constant.weekList.get(1))) {
//                                                if(null !=writeDataList.get(j).getTwo()){
//                                                    cNameHashSet = writeDataList.get(j).getTwo();
//                                                    cNameHashSet.add("/"+cName);
//                                                    writeDataList.get(j).setOne(cNameHashSet);
//                                                }
//                                                continue;
//                                            }
//                                            if (week.equals(Constant.weekList.get(2))) {
//                                                if(null !=writeDataList.get(j).getThree()){
//                                                    cNameHashSet = writeDataList.get(j).getThree();
//                                                    cNameHashSet.add("/"+cName);
//                                                    writeDataList.get(j).setOne(cNameHashSet);
//                                                }
//                                                continue;
//                                            }
//                                            if (week.equals(Constant.weekList.get(3))) {
//                                                if(null !=writeDataList.get(j).getFour()){
//                                                    cNameHashSet = writeDataList.get(j).getFour();
//                                                    cNameHashSet.add("/"+cName);
//                                                    writeDataList.get(j).setOne(cNameHashSet);
//                                                }
//                                                continue;
//                                            }
//                                            if (week.equals(Constant.weekList.get(4))) {
//                                                if(null !=writeDataList.get(j).getFive()){
//                                                    cNameHashSet = writeDataList.get(j).getFive();
//                                                    cNameHashSet.add("/"+cName);
//                                                    writeDataList.get(j).setOne(cNameHashSet);
//                                                }
//                                                continue;
//                                            }
//                                            if (week.equals(Constant.weekList.get(5))) {
//                                                if(null !=writeDataList.get(j).getSex()){
//                                                    cNameHashSet = writeDataList.get(j).getSex();
//                                                    cNameHashSet.add("/"+cName);
//                                                    writeDataList.get(j).setOne(cNameHashSet);
//                                                }
//                                                continue;
//                                            }
//                                            if (week.equals(Constant.weekList.get(6))) {
//                                                if(null !=writeDataList.get(j).getSeven()){
//                                                    cNameHashSet = writeDataList.get(j).getSeven();
//                                                    cNameHashSet.add("/"+cName);
//                                                    writeDataList.get(j).setOne(cNameHashSet);
//                                                }
//                                                continue;
//                                            }
//                                    }
//
//                                    }
//                                }
//                            }
//                }
//            }
//        }
//        return writeDataList;
//    }
//










        /**
         * 获取表头标题数据
         * @param readData
         * @return
         */
    public static ReadData getClassroomName(ReadData readData){
        readData.setFirstClassroom(String.valueOf(getClassroomNameHandle(readData.getFirstClassroom())));
        readData.setSecondClassroom(String.valueOf(getClassroomNameHandle(readData.getSecondClassroom())));
        readData.setThirdClassroom(String.valueOf(getClassroomNameHandle(readData.getThirdClassroom())));
        readData.setFourthClassroom(String.valueOf(getClassroomNameHandle(readData.getFourthClassroom())));
        readData.setFifthClassroom(String.valueOf(getClassroomNameHandle(readData.getFifthClassroom())));
        return readData;
    }

    /**
     * 处理表头标题数据
     * @param str
     * @return
     */
    public static StringBuffer getClassroomNameHandle(String str){
        StringBuffer stringBuffer= new StringBuffer();
        char ch;
        if(String.valueOf(str.charAt(0)).equals("实")){
            // 假如第一个字符是实就添加进去  匹配实验楼615
            stringBuffer.append("实");
        }
        for (int i = 0; i < str.length(); i++) {
            ch = str.charAt(i);
            // 假如下表处的字符是0-9则追加到字符串中 48是0的ASCII码
            if((ch >48 && ch<57) || (ch=='-')){
                stringBuffer.append(ch);
            }
        }
        char x = stringBuffer.charAt(stringBuffer.length()-1);
        if(String.valueOf(stringBuffer.charAt(stringBuffer.length()-1)).equals(Constant.KEY_LINE)){
            stringBuffer.deleteCharAt(stringBuffer.length()-1);
        }
        return stringBuffer;
    }






    /**
     * 返回课程那几周  也即周数
     * @param indexs    特殊字符的下标
     * @param str   待获取的字符
     * @return
     */
    public static List<String> allNum(List<Integer> indexs,String str){
        ArrayList<String> list = new ArrayList<>(); // 存放具体的周数
        String strIndex = null;  // 字符标记 找出自定索引下表为-的字符  做不同的逻辑处理
        str = splitString(str, ":", "(");
        int start;
        int end;
        for (int i = 0; i < indexs.size()-1; i++) {

            if((indexs.get(i+1)-indexs.get(i))>1){
                // 判断字符串特定索引的字符是否是 -  11-14 -> [11,12,13,14]
                if(String.valueOf(str.charAt(indexs.get(i))).equals(Constant.KEY_LINE)){
                    strIndex = Constant.KEY_LINE;
                }
                list.add(str.substring(indexs.get(i)+1,indexs.get(i+1)));
                // 存放周数
                hashSet.add(str.substring(indexs.get(i)+1,indexs.get(i+1)));
            }
        }
        // 假如指定索引位置为-的话  获取list中的两个数字
        // 然后将list清空  将数字变成list 如 6,9  -> [6,7,8,9]
        if(strIndex == Constant.KEY_LINE){
           start = Integer.valueOf(list.get(0));
           end = Integer.valueOf(list.get(1));
           list.clear();
            for (int i = start; i <= end; i++) {
                list.add(String.valueOf(i));
                // 存放周数
                hashSet.add(String.valueOf(i));
            }
        }
        return list;
    }


    /**
     * 修复版本
     *
     * 返回课程那几周  也即周数
     * @param str   待获取的字符
     * @return
     */

    public static List<String> allNum2(String str){
        String keyZ = "周数:";
        String keyJS = "教师:";
        List<Integer> listZ = DataUtils.searchOneChatIndex(keyZ, str);
        List<Integer> listJS = DataUtils.searchOneChatIndex(keyJS, str);
        ArrayList<Integer> list = new ArrayList<>();  //特殊字符索引
        ArrayList<String> week = new ArrayList<>();  // 具体周数
        for (int i = 0; i < listZ.size(); i++) {

            String substring = str.substring(listZ.get(i), listJS.get(i));
            //情况一：周数:5-7周(单),3周,8-14周(4-5节)  有逗号
            //情况二：周数:5-7周(单)    无逗号
            String splitCommaWeek = "";
            //按,分割
            List<Integer> indexCommaList = DataUtils.searchOneChatIndex(",", substring);
            // 如果无逗号
            if(indexCommaList.size()==0){
                if(substring.contains("单")){
                    // 是单周
                    List<Integer> allIndex = DataUtils.searchAllIndex(substring);
                    List<String> allNum = DataUtils.allNum(allIndex, substring);
                    for (String s : allNum) {
                        if(Integer.valueOf(s)%2==0){
                            allNum.remove(s);
                        }
                    }
                    for (String s : allNum) {
                        week.add(s);
                    }
                }else {
                    // 不是单周
                    List<Integer> allIndex = DataUtils.searchAllIndex(substring);
                    List<String> allNum = DataUtils.allNum(allIndex, substring);
                    for (String s : allNum) {
                        week.add(s);
                    }

                }

                // 如果有逗号
            }else {
                // 周数:5-7周(单),3周,8-14周(4-5节)  变成
                // 周数:5-7周(单)     :,3周(     :,8-14周(4-5节)(
                for (int j = 0; j <= indexCommaList.size(); j++) {
                    if(j==0){

                        splitCommaWeek = substring.substring(0,indexCommaList.get(0));
                        splitCommaWeek = splitCommaWeek+"(";
                    }else if(j==indexCommaList.size()){
                        splitCommaWeek = substring.substring(indexCommaList.get(j-1),substring.length()-1);
                        splitCommaWeek = ":"+splitCommaWeek+"(";
                    } else {
                        splitCommaWeek = substring.substring(indexCommaList.get(j-1),indexCommaList.get(j));
                        splitCommaWeek = ":"+splitCommaWeek+"(";
                    }
                    if(splitCommaWeek.contains("单")){
                        // 是单周
                        List<Integer> allIndex = DataUtils.searchAllIndex(splitCommaWeek);
                        List<String> allNum = DataUtils.allNum(allIndex, splitCommaWeek);
                        for (String s : allNum) {
                            if(Integer.valueOf(s)%2==0){
                                allNum.remove(s);
                            }
                        }
                        for (String s : allNum) {
                            week.add(s);
                        }
                    }else {
                        // 不是单周
                        List<Integer> allIndex = DataUtils.searchAllIndex(splitCommaWeek);
                        List<String> allNum = DataUtils.allNum(allIndex, splitCommaWeek);
                        for (String s : allNum) {
                            week.add(s);
                        }
                    }
                }
            }

        }


        return week;
    }


















    /**
     * 返回截取的字符串
     * 如 周数:16周(1-3节)  教师:  截取后字串为 :16周
     * @param string    待截取字符串
     * @param start     截取的开始字符
     * @param end       截取的最后字符
     * @return
     */
    public static String splitString(String string,String start,String end){
        int startIndex = string.indexOf(start);
        int endIndex = string.indexOf(end);
        String substring = string.substring(startIndex, endIndex);
        return substring;
    }



    /**
     * 返回: , - 周  这四种字符排序好的索引
     * @param str  待截取的字符串  课表中的详细信息
     * @return
     */
    public static List<Integer> searchAllIndex(String str) {
        // 用于存放 : , - 周  这四种字符的索引
        ArrayList<Integer> list = new ArrayList<>();
        str = splitString(str, ":", "(");
        // 方式一：:16周
        // 方式二：:15-16周
        // 方式三：:5周,9周,13周
        list.addAll(searchOneChatIndex(Constant.kEY_COLON,str));  // 冒号：下标
        list.addAll(searchOneChatIndex(Constant.KEY_ZHOU,str));   // 周的下标
        list.addAll(searchOneChatIndex(Constant.KEY_COMMA,str));  // ,的下标
        list.addAll(searchOneChatIndex(Constant.KEY_LINE,str));   // -的下标
        Collections.sort(list);
        return list;
    }





    /**
     * 获取某个字符全部索引的方法
     * @param key   要获取全部索引的某个字符 比如“周”
     * @param str   带获取的字符
     * @return
     */
    public static List<Integer> searchOneChatIndex(String key, String str){
        ArrayList<Integer> list = new ArrayList<>();
        int a = str.indexOf(key);//*第一个出现的索引位置
        while (a != -1) {
            list.add(a);
            a = str.indexOf(key, a + 1);//*从这个索引往后开始第一个出现的位置
        }
        return list;
    }


    /**
     * 返回排序好的周数
     * @return
     */
    public static List<Integer> weekRange(){
        HashSet<String> hashSet = DataUtils.hashSet;
        ArrayList<Integer> arrayList = new ArrayList<>();
        Iterator<String> iterator = hashSet.iterator();

        while (iterator.hasNext()) {
            arrayList.add(Integer.valueOf(iterator.next()));
        }
        Collections.sort(arrayList);
        return arrayList;
    }


}
